package com.linux.huhx.business.action;

import com.linux.huhx.base.BaseController;
import com.linux.huhx.utils.EncryptUtils;
import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


/**
 * 文件上传
 * @author huhx
 * @date: 2016年12月20日 下午3:48:25
 */
public class FileUploadAction extends BaseController {

	@Override
	protected Map handleInner(HttpServletRequest request, HttpServletResponse response) throws Exception {
		MultipartFile[] files = new MultipartFile[5];
		System.out.println(files.length);
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String name = request.getParameter("name");
		System.out.println(name + " " + request.getParameter("address"));

		System.out.println(username + ", " + password);
		for (MultipartFile file : files) {
			String realPath = request.getSession().getServletContext().getRealPath("file");
			String newFileContent = file.getOriginalFilename() + file.getSize();
			String newEncryFile = EncryptUtils.sha1Encrypt(newFileContent);

			if (newEncryFile.equals(request.getSession().getAttribute("fileCheck"))) {
				return null;
			}
			try {
				FileUtils.copyInputStreamToFile(file.getInputStream(), new File(realPath, file.getOriginalFilename()));
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			String fileContent = file.getOriginalFilename() + file.getSize();
			String encryFile = EncryptUtils.sha1Encrypt(fileContent);
			request.getSession().setAttribute("fileCheck", encryFile);

			System.out.println(file.getName());
		}

		Map<String, String> map = new HashMap<>();
		return null;
	}
}
